var e = require("../../@babel/runtime/helpers/defineProperty"), r = getApp().globalData.api;

Page({
    data: {
        miniProgram: null,
        isLogin: 0,
        needlogin:!0,
        showModal: !1,
        nickName: "555",
        nickName1: "555",
        mobile:"",
        avatarUrl: "/image/user/defaultUh.png",
        show:0,
        phoneshow:false,

        testName: getApp().globalData.testName,
        communityList: [ {
            id: 11,
            online: !0,
            img: "/image/user/cyxx.png",
            title: "我的积分",
            url: "/subpackages/user-community/pages/integral/integral"
        }, {
            id: 12,
            online: !0,
            img: "/image/user/cyxx.png",
            title: "我的粉丝",
            url: "/subpackages/user-community/pages/fans/fans"
        }, {
            id: 13,
            online: !0,
            img: "/image/user/cyxx.png",
            title: "粉丝订单",
            url: "/subpackages/user-community/pages/fans-order/fans-order"
        }, {
            id: 14,
            online: !0,
            img: "/image/user/yssh.png",
            title: "医生审核",
            url: "/subpackages/user-community/pages/doctor-review/doctor-review"
        }, {
            id: 15,
            online: !0,
            img: "/image/user/cyxx.png",
            title: "下级订单",
            url: "/subpackages/user-community/pages/lower-order/lower-order"
        } ],
        celltwo: [
            {
                id: 21,
                online: !0,
                img: "/image/user/dfk.png",
                msgNum: 0,
                title: "全部",
                url: "/pages/usercenter/userOrder/my-orderlist/my-orderlist?condition=0"
            },
             {
            id: 21,
            online: !0,
            img: "/image/user/dfk.png",
            msgNum: 0,
            title: "待付款",
            url: "/pages/usercenter/userOrder/my-orderlist/my-orderlist?condition=1"
        }, {
            id: 22,
            online: !0,
            img: "/image/user/dyy.png",
            msgNum: 0,
            title: "待预约",
            url: "/pages/usercenter/userOrder/my-orderlist/my-orderlist?condition=2"
        },
        //  {
        //     id: 23,
        //     online: !0,
        //     img: "/image/user/dpj.png",
        //     msgNum: 0,
        //     title: "待评价",
        //     url: "/pages/usercenter/userOrder/my-orderlist/my-orderlist?condition=3"
        // },
         {
            id: 24,
            online: !0,
            img: "/image/user/dtj.png",
            msgNum: 0,
            title: "退款/售后",
            url: "/pages/usercenter/userOrder/my-orderlist/my-orderlist?condition=4"
        } ],
        cellthree: [ 
        //     {
        //     id: 31,
        //     isShow: !0,
        //     online: !0,
        //     img: "/image/user/yhq.png",
        //     msgNum: 0,
        //     title: "优惠券",
        //     url: "/pages/usercenter/user_property/user_discounts/user_discounts"
        // },
        //  {
        //     id: 32,
        //     isShow: !0,
        //     online: !0,
        //     img: "/image/user/cyxx.png",
        //     msgNum: 0,
        //     title: "常用信息",
        //     url: "/pages/usercenter/user_property/user_contact/contact-list"
        // }, 
        
        {
            id: 33,
            isShow: !0,
            online: !1,
            img: "/image/user/tjbg.png",
            msgNum: 0,
            title: "体检报告",
            url: "/subpackages/user-report/pages/dubaogao/dubaogao"
        },
        //  {
        //     id: 34,
        //     isShow: !0,
        //     online: !0,
        //     img: "/image/user/tjk.png",
        //     msgNum: 0,
        //     title: "体检卡",
        //     url: "/pages/usercenter/user_property/user_cards/user_cards"
        // } 
    
    ],
        cellfour: [ {
            isShow: !0,
            id: 41,
            online: !0,
            img: "/image/user/wdpj.png",
            msgNum: 0,
            title: "我的评价",
            url: "/pages/usercenter/user_record/user_evaluation/user_evaluation"
        }], 
        cellfive: [{
            id: 52,
            online: !1,
            img: "/image/user/bzzx.png",
            msgNum: 0,
            title: "帮助中心",
            url: "/subpackages/user_service/pages/help-center/help-center"
        } ],
        allOrder: {
            url: "/pages/usercenter/userOrder/my-orderlist/my-orderlist",
            online: !0
        },
        wuserinfo: "",
        redEnvelopeList: []
    },
    toUser: function() {
        wx.navigateTo({
            url: "/subpackages/personal/pages/main"
        });
    },
    routerGoIn: function(e) {
        console.log(e.currentTarget.dataset.v);
        var r = this;
        if (e.currentTarget.dataset.v.online) wx.getStorageSync("openid") ? s() : wx.navigateTo({
            url: "/pages/login/login"
        }); else {
            
        }
        function s() {
            getApp().globalData.userInfo = r.data.userData.userInfo, e.currentTarget.dataset.v.url && wx.navigateTo({
                url: e.currentTarget.dataset.v.url
            });
        }
    },
    onLoad: function(e) {
        wx.canIUse("getAccountInfoSync") && this.setData({
            miniProgram: wx.getAccountInfoSync().miniProgram
        });
    },
    onShow: function() {
        console.log(wx.getStorageSync("token"), getApp().globalData.token, "getApp().globalData.token");
        var e = this;
        // this.data.userData || wx.showLoading() || getApp().globalData.token ? r.getPort("getUserPageInfo").then(function(r) {
        //     if (wx.hideLoading(), "SUCCESS" == r.result) {
        //         if (console.log(r, "usercenter"), r.values.hasOwnProperty("userInfo")) {
        //             var t = r.values.userInfo;
        //             getApp().globalData.userInfo = t;
        //         }
        //         e.setData({
        //             isLogin: 1,
        //             userData: r.values,
        //             "celltwo[0].msgNum": r.values.hasOwnProperty("orderStatisticsInfo") ? r.values.orderStatisticsInfo.waitPayCnt : "0",
        //             "celltwo[1].msgNum": r.values.hasOwnProperty("orderStatisticsInfo") ? r.values.orderStatisticsInfo.waitAppointCnt : "0",
        //             "celltwo[2].msgNum": r.values.hasOwnProperty("orderStatisticsInfo") ? r.values.orderStatisticsInfo.waitEvaCnt : "0"
        //         });
        //     }
        // }, function(e) {
        //     wx.hideLoading();
        // }) : (wx.hideLoading(), this.setData({
        //     isLogin: 0
        // })), this.setData({
        //     redEnvelopeList: getApp().globalData.redEnvelopeList
        // });
        this.getUinfo();
    },

    getUinfo: function() {
        let that=this;  
        
        wx.showLoading({
            title: "正在加载中..."
        });
        var openid = wx.getStorageSync('openid');
   
            r.post1("wechat/Member/getUserInfo", {
                openid: openid
            }).then(function(t) {
            if (wx.hideLoading(), "1" == t.code) {
                var a = t.data;
  
                getApp().globalData.userInfo = t.data;


                //    that.setData({ 
                //     userData: t.data,
                  
                // });

                if( t.data.alias_nickname&& t.data.headimgurl){
                    that.setData({avatarUrl:getApp().globalData.baseImgUrl+ t.data.headimgurl,needlogin:0,nickName1: t.data.alias_nickname,mobile:t.data.mobile,userData: t.data}); 
                  
                  }  


                // that.setData({
                //     isLogin: 1,
                //     wuserinfo: fh.data,
                //     // "celltwo[0].msgNum": r.values.hasOwnProperty("orderStatisticsInfo") ? r.values.orderStatisticsInfo.waitPayCnt : "0",
                //     // "celltwo[1].msgNum": r.values.hasOwnProperty("orderStatisticsInfo") ? r.values.orderStatisticsInfo.waitAppointCnt : "0",
                //     // "celltwo[2].msgNum": r.values.hasOwnProperty("orderStatisticsInfo") ? r.values.orderStatisticsInfo.waitEvaCnt : "0"
                // });
 
                console.log("uinfo",a)
                wx.setStorageSync('userinfo', t.data);
                wx.setStorageSync('uid',t.data.id);
                wx.setStorageSync('phoneNumber', t.data.mobile);

                // let img=config.apiwimgURL+ t.data.headimgurl;
                // console.log(img);
                // e.setData({avatarUrl:s.apiwimgURL+ t.data.headimgurl}); 
                //   if( t.data.alias_nickname&& t.data.headimgurl){
                //     e.setData({avatarUrl:s.apiwimgURL+ t.data.headimgurl,needlogin:0,nickName1: t.data.alias_nickname,mobile:t.data.mobile}); 
                 
                //   }  
         
            } else {

                wx.showModal({
                    title: "提示",
                    content: "暂无数据",
                    showCancel: !1
                }); 
            };
        });
    },
    onChooseAvatar(e){
        const { avatarUrl } = e.detail;
        if(avatarUrl){
          wx.showLoading();
          this.headUploadImgs(avatarUrl)
        }
      },

      headUploadImgs(filePath) {
          let that=this;
          //   uploadImg: e.apiwURL + "api/common/index/uploadImg",
          let url=getApp().globalData.baseUrl+"/common/index/uploadImg";
        wx.uploadFile({
          url: url,  
          filePath: filePath,
          name: 'file',
          success: (res) => {
            if (res.data) {
              let json = JSON.parse(res.data)
              if (json.code == 1) {
                that.setData({avatarUrl:filePath});  
                this.headImgWrite(json.data)
              } else {
                wx.showToast({
                  title: '上传失败',
                  icon: 'none'
                })
              }
            } else {
              
            }
          }
        })


      },
      headImgWrite(url) {

        var that=this;
        // return post("wechat/home/setMemberInfo", data);

        var openid = wx.getStorageSync('openid');
   
            r.post1("wechat/home/setMemberInfo", {
                headimgurl:url,
                flag: 2, 
                openid: openid
            }).then(function(t) { 
            if ("1" == t.code) {               
                wx.hideLoading()       
                that.setData({show:true});
                // that.setData({show:true});
            };
        })
      
      }, 

      getNickName(e){
        this.nickname=e.detail.value;
      },
      onSubmit(e){
        let that=this;  
        if(!this.nickname){
          return wx.showToast({
            title: '请填写昵称',
            icon: 'none'
          })
        }
        wx.showLoading();
        var openid = wx.getStorageSync('openid');   
        r.post1("wechat/home/setMemberInfo", {
            alias_nickname:this.nickname,
            flag: 2, 
            openid: openid
        }).then(function(e) { 
            if ("1" == e.code) {

                wx.hideLoading();
                that.setData({show:false});
                wx.showToast({
                  title: '登录成功！',
                  icon: 'success'
                })
                that.getUinfo(); 
            };
        }) 

      },

      qklogin: function(e) {
        // console.log('login');
        // console.log(e);
        wx.showLoading({
            title: "正在登录中..."
        });
        var s = this;
        "getPhoneNumber:ok" == e.detail.errMsg ? a.request(a.HttpMethod.POST, o.apiRoute.gettel, {
            code: e.detail.code,
            sessionKey:  wx.getStorageSync('sessionkey'),
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
        }).then(function(e) {
            if (wx.hideLoading(), "1" == e.code) {
                s.phone=e.data.phoneNumber;
                s.triggerEvent("onShowLogOut"); 
                s.setData({
                    showLoginButton: !1
                }), (0, t.default)({
                    message: "成功",
                    duration: 5e3
                }); 
                s.onSubmitphone(); 
            } else wx.hideLoading(), (0, t.default)({
                message: "登录失败，".concat(e.message),
                duration: 5e3
            });
        }) : (wx.hideLoading(), (0, t.default)({
            message: "请选择手机号",
            duration: 5e3
        }));
    },


      getUsertel: function() {
        // console.log(222222);
        var e = this;
        e.setData({showModal:!0}); 
    },

      bindphone() {
        this.setData({phoneshow:true});
       
      },

      onClosephone() {
        this.setData({phoneshow:false});  
      },
      getphone(e){
        this.phone=e.detail.value;
      },

      onSubmitphone(){
        let that=this;  
        if(!this.phone){
          return wx.showToast({
            title: '请填写',
            icon: 'none'
          })
        }
        wx.showLoading();

        var openid = wx.getStorageSync('openid');   
        r.post1("wechat/home/setMemberInfo", {
            mobile:this.phone,
            flag: 2, 
            openid: openid
        }).then(function(e) {  

            if ("1" == e.code) {

                wx.hideLoading();
                // that.setData({show:false});
                wx.showToast({
                  title: '登录成功！',
                  icon: 'success'
                })
                that.getUinfo(); 
            };
        }) 

      },

        



      onClose() {
        this.setData({show:false});  
      },




    binderrorimg: function(r) {
        var t = r.currentTarget.dataset.errorimg;
        this.setData(e({}, t, "/image/user/defaultUh.png"));
    },
    onShareAppMessage: function(e) {}
});